#include <Centipede.h>
#include <Wire.h>
#include "HALoutput.h"
#define OUT_GROUP2      0
#define OUT_GROUP1      1
#define OUT_STROBE      2
#define OUT_KEYSELECT   3
#define OUT_BUZZER      4
#define OUT_HEATER      5
#define OUT_SPEED2      6
#define OUT_SPEED1      7
#define OUT_DATAC       8
#define OUT_DATAB       9
#define OUT_DATAA       10
#define OUT_MOTOR_RL    11
#define OUT_SOAP1       12
#define OUT_SINK        13
#define OUT_DRAIN       14
#define OUT_LOCK        15



HALoutput::HALoutput(Centipede CP)
{
  CS = CP;
}

void HALoutput::toggle_engine(bool reverseDirection, int Speed)
{
  if(reverseDirection)
  {
    CS.digitalWrite(OUT_MOTOR_RL, HIGH);
  }
  else
  {
    CS.digitalWrite(OUT_MOTOR_RL, LOW);
  }
  switch(Speed)
  {
  case 1:
    CS.digitalWrite(OUT_SPEED1, HIGH);
    CS.digitalWrite(OUT_SPEED2, LOW);
    break;
  case 2:
    CS.digitalWrite(OUT_SPEED1, LOW);
    CS.digitalWrite(OUT_SPEED2, HIGH);
    break;
  case 3:
    CS.digitalWrite(OUT_SPEED1, LOW);
    CS.digitalWrite(OUT_SPEED2, LOW);
    break;
  default:
    CS.digitalWrite(OUT_SPEED1, HIGH);
    CS.digitalWrite(OUT_SPEED2, HIGH);
    break;
  }

}
void HALoutput::toggle_heater(bool enabled)
{
  if(enabled)
  {
    CS.digitalWrite(OUT_HEATER, LOW);
  }
  else
  {
    CS.digitalWrite(OUT_HEATER, HIGH);
  }
}

void HALoutput::toggle_water_supply(bool enabled)
{
  if(enabled)
  {
    CS.digitalWrite(OUT_DRAIN, HIGH);
  }
  else
  {
    CS.digitalWrite(OUT_DRAIN, LOW);
  }
}
void HALoutput::toggle_water_sink(bool enabled)
{
  if(enabled)
  {
    CS.digitalWrite(OUT_SINK, HIGH);
  }
  else
  {
    CS.digitalWrite(OUT_SINK, LOW);
  }
}


void HALoutput::set_coin_leds(int amount)
{
  int tens = 0;
  int fiftys = 0;
  int twohundreds = 0;

  while(amount -200 >= 0)
  {
    amount -=200;
    twohundreds++; 
  }
  while(amount - 50 >= 0)
  {
    amount -=50;
    fiftys++; 
  }
  while(amount - 10 >= 0)
  {
    amount -=10;
    tens++;
  }
  show_amount_ten_coins(tens);
  show_amount_fifty_coins(fiftys);
  show_amount_two_hundred_coins(twohundreds);
}
void HALoutput::show_amount_fifty_coins(int amount)
{
  CS.digitalWrite(OUT_STROBE,LOW);
  CS.digitalWrite(OUT_GROUP1, HIGH);
  CS.digitalWrite(OUT_GROUP2, LOW);
  switch(amount)
  {
  case 1:
    CS.digitalWrite(OUT_DATAA, HIGH);
    CS.digitalWrite(OUT_DATAB, LOW);
    CS.digitalWrite(OUT_DATAC, LOW);
    break;
  case 2:
    CS.digitalWrite(OUT_DATAA, LOW);
    CS.digitalWrite(OUT_DATAB, HIGH);
    CS.digitalWrite(OUT_DATAC, LOW);
    break;
  case 3:
    CS.digitalWrite(OUT_DATAA, LOW);
    CS.digitalWrite(OUT_DATAB, LOW);
    CS.digitalWrite(OUT_DATAC, HIGH);
    break;
  }
  delay(81);
  CS.digitalWrite(OUT_STROBE,HIGH);
  delay(11);
}
void HALoutput::show_amount_ten_coins (int amount)
{
  CS.digitalWrite(OUT_STROBE,LOW);
  CS.digitalWrite(OUT_GROUP1, LOW);
  CS.digitalWrite(OUT_GROUP2, LOW);
  switch(amount)
  {
  case 0:
    {
      CS.digitalWrite(OUT_DATAA, LOW);
      CS.digitalWrite(OUT_DATAB, LOW);
      CS.digitalWrite(OUT_DATAC, LOW);
      break;
    }
  case 1:
    CS.digitalWrite(OUT_DATAA, HIGH);
    CS.digitalWrite(OUT_DATAB, LOW);
    CS.digitalWrite(OUT_DATAC, LOW);
    break;
  case 2:
    CS.digitalWrite(OUT_DATAA, LOW);
    CS.digitalWrite(OUT_DATAB, HIGH);
    CS.digitalWrite(OUT_DATAC, LOW);
    break;
  case 3:
    CS.digitalWrite(OUT_DATAA, LOW);
    CS.digitalWrite(OUT_DATAB, LOW);
    CS.digitalWrite(OUT_DATAC, HIGH);
    break;
  case 4:
    CS.digitalWrite(OUT_DATAA, HIGH);
    CS.digitalWrite(OUT_DATAB, LOW);
    CS.digitalWrite(OUT_DATAC, HIGH);
    break; 

  }
  delay(81);
  CS.digitalWrite(OUT_STROBE,HIGH);
  delay(11);
}

void HALoutput::show_amount_two_hundred_coins(int amount)
{
  CS.digitalWrite(OUT_STROBE,LOW);
  CS.digitalWrite(OUT_GROUP1, LOW);
  CS.digitalWrite(OUT_GROUP2, HIGH);
  switch(amount)
  {
  case 1:
    CS.digitalWrite(OUT_DATAA, HIGH);
    CS.digitalWrite(OUT_DATAB, LOW);
    CS.digitalWrite(OUT_DATAC, LOW);
    break;
  case 2:
    CS.digitalWrite(OUT_DATAA, LOW);
    CS.digitalWrite(OUT_DATAB, HIGH);
    CS.digitalWrite(OUT_DATAC, LOW);
    break;
  case 3:
    CS.digitalWrite(OUT_DATAA, HIGH);
    CS.digitalWrite(OUT_DATAB, HIGH);
    CS.digitalWrite(OUT_DATAC, LOW);
    break;

  } 
  delay(81);
  CS.digitalWrite(OUT_STROBE,HIGH);
  delay(11);
}
void HALoutput::set_soap_box(int soap_number, bool enabled)
{
  if(enabled)
  {
    switch(soap_number)
    {
    case 2:
      CS.digitalWrite(OUT_STROBE,LOW);
      CS.digitalWrite(OUT_GROUP1, LOW);
      CS.digitalWrite(OUT_GROUP2, HIGH);
      CS.digitalWrite(OUT_DATAC, HIGH);
      delay(82);
      CS.digitalWrite(OUT_STROBE,HIGH);
      delay(10);
      break;
    default:
      CS.digitalWrite(OUT_SOAP1, HIGH);
      break;
    }
  }
  else
  {
    switch(soap_number)
    {
    case 2:
      CS.digitalWrite(OUT_STROBE,LOW);
      CS.digitalWrite(OUT_GROUP1, LOW);
      CS.digitalWrite(OUT_GROUP2, HIGH);
      CS.digitalWrite(OUT_DATAC, LOW);
      delay(81);
      CS.digitalWrite(OUT_STROBE,HIGH);
      delay(10);
      break;
    default:
      CS.digitalWrite(OUT_SOAP1, LOW);
      break;
    }
  }
}

void HALoutput::toggle_program_leds(int program)
{
  switch(program)
  {
  case 1:
    CS.digitalWrite(OUT_STROBE,LOW);
    CS.digitalWrite(OUT_GROUP1, HIGH);
    CS.digitalWrite(OUT_GROUP2, HIGH);
    CS.digitalWrite(OUT_DATAC, LOW);
    CS.digitalWrite(OUT_DATAB, LOW);
    CS.digitalWrite(OUT_DATAA, HIGH);
    delay(82);
    CS.digitalWrite(OUT_STROBE,HIGH);
    delay(10);
    break;
  case 2:
    CS.digitalWrite(OUT_STROBE,LOW);
    CS.digitalWrite(OUT_GROUP1, HIGH);
    CS.digitalWrite(OUT_GROUP2, HIGH);
    CS.digitalWrite(OUT_DATAC, LOW);
    CS.digitalWrite(OUT_DATAB, HIGH);
    CS.digitalWrite(OUT_DATAA, LOW);
    delay(82);
    CS.digitalWrite(OUT_STROBE,HIGH);
    delay(10);
    break;
  case 3:
    CS.digitalWrite(OUT_STROBE,LOW);
    CS.digitalWrite(OUT_GROUP1, HIGH);
    CS.digitalWrite(OUT_GROUP2, HIGH);
    CS.digitalWrite(OUT_DATAC, HIGH);
    CS.digitalWrite(OUT_DATAB, LOW);
    CS.digitalWrite(OUT_DATAA, LOW);
    delay(82);
    CS.digitalWrite(OUT_STROBE,HIGH);
    delay(10);
    break;
  default:
    CS.digitalWrite(OUT_STROBE,LOW);
    CS.digitalWrite(OUT_GROUP1, HIGH);
    CS.digitalWrite(OUT_GROUP2, HIGH);
    CS.digitalWrite(OUT_DATAC, LOW);
    CS.digitalWrite(OUT_DATAB, LOW);
    CS.digitalWrite(OUT_DATAA, LOW);
    delay(82);
    CS.digitalWrite(OUT_STROBE,HIGH);
    delay(10);
    break;
  }

}

void HALoutput::toggle_lock_led(bool enabled)
{
  if(enabled)
  {
    CS.digitalWrite(OUT_LOCK, HIGH);
  }
  else
  {
    CS.digitalWrite(OUT_LOCK, LOW);
  }
}

